package fr.insta.browser.home;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import fr.insta.browser.domain.StateOnStart;
import fr.insta.browser.home.SetUpHomePanel.SetUpHomeActions;
import fr.insta.browser.service.tab.ITabService;
import fr.insta.browser.view.tab.ContentTab;
import fr.insta.webview.factory.WebObjectFactory;
import fr.insta.webview.object.IWebObject;
import fr.insta.webview.service.ServiceWebObject;

/**
 * <p>SetUpHomeListener class.</p>
 *
 * @author Once
 * @version $Id: $Id
 */
public class SetUpHomeListener implements ActionListener 
{
	private SetUpHomePanel panel;
	private SetUpHomeActions action;
	private ITabService tabService;

	/**
	 * <p>Constructor for SetUpHomeListener.</p>
	 *
	 * @param _panel a {@link fr.insta.browser.home.SetUpHomePanel} object.
	 * @param _action a {@link fr.insta.browser.home.SetUpHomePanel.SetUpHomeActions} object.
	 */
	public SetUpHomeListener(SetUpHomePanel _panel, SetUpHomeActions _action, ITabService _tabService)
	{
		this.panel = _panel;
		this.action = _action;
		this.tabService = _tabService;
	}

	/** {@inheritDoc} */
	@Override
	public void actionPerformed(ActionEvent event) 
	{
		switch (action) 
		{
			case SAVE_CHANGES:
				IWebObject webObject = WebObjectFactory.getBookmarkFactory().createNewWebObject("", panel.getTxt_currentHome().getText());
				ServiceWebObject.INSTANCE.serializeHomepage(webObject);
				panel.getParentFrame().dispose();
				break;

			case CANCEL:
				panel.getParentFrame().dispose();
				break;

			case CURRENT_PAGE:
				panel.getTxt_currentHome().setText(((ContentTab)tabService.getCurrentTab()).getWebView().getUrl());
				break;

			case SET_DEFAULT_HOME:
				panel.getTxt_currentHome().setText(Home.DEFAULT_HOMEPAGE);
				break;

			default:
				break;
		}
	}
}
